/*
 * Powered By www.cardmee.net
 */
package com.ns.school.service.wave.impl;

import com.ns.school.common.utils.DateUtil;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ns.school.po.edu.BorrowBookDB;
import com.ns.school.po.edu.LayuiResult;
import com.ns.school.po.sys.User;
import com.ns.school.po.wave.WaveDevicePO;
import lombok.extern.slf4j.Slf4j;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ns.school.mapper.wave.WaveRequestDetailMapper;
import com.ns.school.po.wave.WaveRequestDetailPO;
import com.ns.school.common.base.R;
import com.ns.school.service.base.BaseService;
import com.ns.school.service.wave.WaveRequestDetailService;

/**
 * @author wt-97
 */
@Slf4j
@Service
public class WaveRequestDetailServiceImpl extends BaseService implements WaveRequestDetailService {

    @Autowired
    private WaveRequestDetailMapper waveRequestDetailMapper;



    /**
     * 查询全部
     */
    @Override
    public List<WaveRequestDetailPO> queryList(WaveRequestDetailPO params){
        return waveRequestDetailMapper.queryList(params);
    }

    /**
     * 查询全部(分页)
     */
    @Override
    public R<List<?>> queryPageList(WaveRequestDetailPO params){
        R<List<?>> r = new R<List<?>>();
        startPage(params);
        List<WaveRequestDetailPO> list = waveRequestDetailMapper.queryList(params);
        for (WaveRequestDetailPO cur : list) {
            cur.setEncryptionId(encode(cur.getId()));
        }
        r.setData(list);
        r.setCount(new PageInfo<>(list).getTotal());
        return r;
    }

    /**
     * 查询总数
     */
    @Override
    public int queryCount(WaveRequestDetailPO params){
        return waveRequestDetailMapper.queryCount(params);
    }

    /**
     * 新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<?> insert(WaveRequestDetailPO params)  {
        params.setCreateTime(DateUtil.getNowDate());
        if (waveRequestDetailMapper.insertSelective(params) == 0) {
            transactionRollBack();
            return R.error("新增失败");
        }
        return R.success();
    }

    /**
     * 批量新增
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<?> insertBatch(List<WaveRequestDetailPO> list) {
        if (waveRequestDetailMapper.insertBatch(list) == 0) {
            transactionRollBack();
            return R.error("批量新增失败");
        }
        return R.success();
    }

    /**
     * 更新
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<?> update(WaveRequestDetailPO params)  {
       // params.setUpdateTime(DateUtil.getNowDate());
        if (waveRequestDetailMapper.updateById(params) == 0) {
            transactionRollBack();
            return R.error("更新失败");
        }
        return R.success();
    }

    /**
     * 删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<?> updateDel(WaveRequestDetailPO params)  {
        if (waveRequestDetailMapper.updateDel(params) == 0) {
            transactionRollBack();
            return R.error("删除失败");
        }
        return R.success();
    }

    @Override
    public List<WaveDevicePO> queryAllName(Map<String, Object> map) {
        return waveRequestDetailMapper.queryAllName(map);
    }

    @Override
    public List<User> selectAllNames(Map<String, Object> map) {
        return waveRequestDetailMapper.selectAllName(map);
    }

    @Override
    public WaveRequestDetailPO selectWaveRequest(Integer id) {
        return waveRequestDetailMapper.selectWaveRequest(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<?> updateDelAll(WaveRequestDetailPO params, String ids) {
        String[] strarray = ids.split(",");
        //删除
        for (int i = 0; i < strarray.length; i++) {
            if (!strarray[i].isEmpty()) {
                if(waveRequestDetailMapper.updateDelAll(Integer.valueOf(strarray[i]))== 0){
                    transactionRollBack();
                    return R.error("删除失败");
                }

            }
        }
        return R.success();
    }

    @Override
    public int checkRequest(WaveRequestDetailPO waveRequestDetailPO) {
        return waveRequestDetailMapper.checkRequest(waveRequestDetailPO);
    }

    @Override
    public void auditRequest(LayuiResult result,Integer id) {
        // 获取当前日期和时间

        int cont=waveRequestDetailMapper.checkRequestUpdate(result,id);
        if(cont>0){
            result.setCode(0);
            result.setMsg("审核通过");
        }

    }

    @Override
    public int checkRequestFail(Integer id) {
        return waveRequestDetailMapper.checkRequestFail(id);
    }
}
